#include "infoinquire.h"
#include "ui_infoinquire.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QSqlRecord>
#include <QLabel>
#include <QMouseEvent>




InfoInquire::InfoInquire(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::InfoInquire)
{

    ui->setupUi(this);

    //事件过滤函数(键盘)的开启
    softKeyboard = new SoftKeyboard();
    ui->lineEdit->installEventFilter(this);
    ui->lineEdit_2->installEventFilter(this);
    ui->lineEdit_3->installEventFilter(this);
    ui->lineEdit_4->installEventFilter(this);

    //信息窗口点击事件处理
    connect(ui->tableWidget,&QTableWidget::cellClicked,this,&InfoInquire::onCellClicked);
    connect(ui->tableWidget_2,&QTableWidget::cellClicked,this,&InfoInquire::onCellClicked_2);

    //初始化界面信息
    QString selectSql=QString("select * from ininfo;");
    InTableSelect(selectSql);
    selectSql=QString("select * from outinfo;");
    OutTableSelect(selectSql);
    ParkingSelect();
    FreeSelect();
}


InfoInquire::~InfoInquire()
{
    delete ui;
}


//更改数据接口 updata 表名 set 字段名称=字段值 where 条件

/*
    //打包sql插入语句
    QString insertSql=QString("insert into ininfo values('D:/share/c++/qtcpp/build-serverdevice-Desktop_Qt_5_14_2_MinGW_64_bit-Debug/pic.png','粤GP68622','是','B301','2024-8-6 10:54:00');");
    qDebug()<<insertSql;
    //执行sql语句
    QSqlQuery query;  //自动关联到默认连接的数据上
    if(!query.exec(insertSql))
    {
        QMessageBox::warning(this,"sql","插入失败");
        qDebug()<<query.lastError().text();
    }else
    {
        QMessageBox::warning(this,"sql","插入成功");
    }

    insertSql=QString("insert into outinfo values('2','粤GP42612','2024-8-6 10:54:00','2024-8-6 10:54:30',50);");
    qDebug()<<insertSql;
    //执行sql语句
    if(!query.exec(insertSql))
    {
        QMessageBox::warning(this,"sql","插入失败");
        qDebug()<<query.lastError().text();
    }else
    {
        QMessageBox::warning(this,"sql","插入成功");
    }
*/




//查询车辆信息并显示到tablewidget
void InfoInquire::on_pushButton_2_clicked()
{
    QString selectSql;
    //打包sql查询语句
    if(ui->lineEdit->text().isEmpty())
    {
        selectSql=QString("select * from ininfo");
    }
    else
    {
        selectSql=QString("select * from ininfo  where license like '%1%' or location like '%2%' or time = '%3';")
                .arg(ui->lineEdit->text())
                .arg(ui->lineEdit->text())
                .arg(ui->lineEdit->text());

    }
    InTableSelect(selectSql);

    if(ui->lineEdit->text().isEmpty())
    {
        selectSql=QString("select * from outinfo");
    }
    else
    {
        selectSql=QString("select * from outinfo  where license like '%1%' or outtime = '%3';")
                .arg(ui->lineEdit->text())
                .arg(ui->lineEdit->text())
                .arg(ui->lineEdit->text());

    }
    OutTableSelect(selectSql);

}


//查询空闲车位
void InfoInquire::FreeSelect()
{
    QString selectSql=QString("select * from parkingstatus where status='空闲';");
    QSqlQuery query;
    if(!query.exec(selectSql))
    {
        qDebug()<<query.lastError().text();
        return ;
    }
     QSqlRecord record = query.record();
     int i=0;
     while(query.next())
     {
         i++;
     }
     ui->label_3->setText(QString::number(i));

}

//查询总车位
void InfoInquire::ParkingSelect()
{
    QString selectSql=QString("select * from parkingstatus;");
    QSqlQuery query;
    if(!query.exec(selectSql))
    {
        qDebug()<<query.lastError().text();
        return ;
    }
     QSqlRecord record = query.record();
     int i=0;
     while(query.next())
     {
         i++;
     }
     ui->label_6->setText(QString::number(i));

}

//执行搜查函数并添加到tablewidget
void InfoInquire::InTableSelect(QString selectSql)
{
    //执行sql语句
    QSqlQuery query;  //自动关联到默认连接的数据上
    if(!query.exec(selectSql)) //判断语句是否执行成功
    {
        qDebug()<<query.lastError().text();
    }
    //设置行数不可见
    QHeaderView *verticalHeader = ui->tableWidget->verticalHeader();
    // 设置垂直头视图为不可见
    verticalHeader->setVisible(false);

    QSqlRecord record = query.record();
    ui->tableWidget->setColumnCount(record.count());
    //定义列表标题
    QStringList headers;
    headers<<"图片"<<"识别车牌"<<"在库中"<<"停车地点"<<"入库时间";
    ui->tableWidget->setHorizontalHeaderLabels(headers);

    row=1;
    //获取下一条数据next为NULL表示没有数据
    while(query.next())
    {
        //获取记录
        QSqlRecord record = query.record();
        //给每列信息写入数据
        ui->tableWidget->setRowCount(row);
        for(int i=0; i<5; i++)
        {
            QTableWidgetItem *item = new QTableWidgetItem(record.value(i).toString());
            ui->tableWidget->setItem(row-1,i,item);
        }
        row++;
        // 调整列宽以适应内容
        ui->tableWidget->resizeColumnsToContents();

    }

    // 检查是否有数据被添加
    if (row == 1) {

        //清空tablewidget
        ui->tableWidget->clearContents();
        ui->tableWidget->setRowCount(0);
        ui->tableWidget->setColumnCount(0);
    }

}


//出库信息表
void InfoInquire::OutTableSelect(QString selectSql)
{
    //执行sql语句
    QSqlQuery query;  //自动关联到默认连接的数据上
    if(!query.exec(selectSql)) //判断语句是否执行成功
    {
        qDebug()<<query.lastError().text();
    }
    //设置行数不可见
    QHeaderView *verticalHeader = ui->tableWidget_2->verticalHeader();
    // 设置垂直头视图为不可见
    verticalHeader->setVisible(false);

    QSqlRecord record = query.record();
    ui->tableWidget_2->setColumnCount(record.count());
    //定义列表标题
    QStringList headers;
    headers<<"图片"<<"识别车牌"<<"入库时间"<<"出库时间"<<"停车费用";
    ui->tableWidget_2->setHorizontalHeaderLabels(headers);

    int outrow=1;
    //获取下一条数据next为NULL表示没有数据
    while(query.next())
    {
        //获取记录
        QSqlRecord record = query.record();
        //给每列信息写入数据
        ui->tableWidget_2->setRowCount(outrow);
        for(int i=0; i<5; i++)
        {
            QTableWidgetItem *item = new QTableWidgetItem(record.value(i).toString());
            ui->tableWidget_2->setItem(outrow-1,i,item);
        }
        outrow++;

    }
    // 调整列宽以适应内容
    ui->tableWidget_2->resizeColumnsToContents();
    if (outrow == 1) {
        if(row==1)
        {
            QMessageBox::warning(this, "selecterror", "没有对应项");
        }
        ui->tableWidget_2->clearContents();
        ui->tableWidget_2->setRowCount(0);
        ui->tableWidget_2->setColumnCount(0);
    }

}

//车位查询


//显示点击地方的图片
void InfoInquire::onCellClicked(int row, int column)
{
    // 获取点击单元格的项
    for(int i=0;i<5;i++)
    {
        QTableWidgetItem *item = ui->tableWidget->item(row, i);
        QTableWidgetItem *headitem = ui->tableWidget->horizontalHeaderItem(i);
        if (i==0)
        {
            // 显示单元格数据
            qDebug() << "Data in clicked cell:" << item->text();
            QPixmap mmp(item->text());
            mmp=mmp.scaled(ui->label->size());
            ui->label->setPixmap(mmp);
        }
        else if(i==1)
        {
           ui->label_13->setText(item->text());
        }
        else if(i==2)
        {
           ui->label_14->setText(headitem->text());
           ui->label_15->setText(item->text());
        }
        else if(i==3)
        {
            ui->label_18->setText(headitem->text());
            ui->label_21->setText(item->text());
        }
        else if(i==4)
        {
            ui->label_16->setText(headitem->text());
            ui->label_17->setText(item->text());
        }
    }
}

//显示点击地方的图片
void InfoInquire::onCellClicked_2(int row, int column)
{
    // 获取点击单元格的项
    for(int i=0;i<5;i++)
    {
        QTableWidgetItem *item = ui->tableWidget_2->item(row, i);
        QTableWidgetItem *headitem = ui->tableWidget_2->horizontalHeaderItem(i);
        if (i==0)
        {
            // 显示单元格数据
            qDebug() << "Data in clicked cell:" << item->text();
            QPixmap mmp(item->text());
            mmp=mmp.scaled(ui->label->size());
            ui->label->setPixmap(mmp);
        }
        else if(i==1)
        {
           ui->label_13->setText(item->text());
        }
        else if(i==2)
        {
           ui->label_14->setText(headitem->text());
           ui->label_15->setText(item->text());
        }
        else if(i==3)
        {
            ui->label_18->setText(headitem->text());
            ui->label_21->setText(item->text());
        }
        else if(i==4)
        {
            ui->label_16->setText(headitem->text());
            ui->label_17->setText(item->text());
        }
    }
}

//事件过滤器处理函数(键盘)
bool InfoInquire::eventFilter(QObject *watched, QEvent *event)
{
    //添加键盘
    if(event->type()==QEvent::MouseButtonRelease)//事件类型
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);//将事件强制转换
        if(mouseEvent->button() == Qt::LeftButton)
        {
            QPoint point = this->pos();
            if(watched == ui->lineEdit)
            {
                softKeyboard->hideInputBufferArea(ui->lineEdit);
                softKeyboard->resize(600,320);


                // 获取lineEdit的全局位置
                QPoint lineEditGlobalPos = ui->lineEdit->mapToGlobal(QPoint(0, 0));
                // 计算softKeyboard的顶部位置
                int newY = lineEditGlobalPos.y() +ui->lineEdit->height();
                // 移动softKeyboard到lineEdit的上方
                softKeyboard->move(point.x() + (this->width() - 600) / 2, newY);
                softKeyboard->setMoveEnabled(false);
                softKeyboard->show();
            }

            if(watched == ui->lineEdit_2)
            {
                softKeyboard->hideInputBufferArea(ui->lineEdit_2);
                softKeyboard->resize(600,320);

                // 计算softKeyboard的顶部位置
                int newY = point.y()+80;
                // 移动softKeyboard到lineEdit的上方
                softKeyboard->move(point.x() , newY);
                softKeyboard->setMoveEnabled(false);
                softKeyboard->show();
            }

            if(watched == ui->lineEdit_3)
            {
                softKeyboard->hideInputBufferArea(ui->lineEdit_3);
                softKeyboard->resize(600,320);

                // 计算softKeyboard的顶部位置
                int newY =point.y()+80;
                // 移动softKeyboard到lineEdit的上方
                softKeyboard->move(point.x() ,  newY);
                softKeyboard->setMoveEnabled(false);
                softKeyboard->show();
            }

            if(watched == ui->lineEdit_4)
            {
                softKeyboard->hideInputBufferArea(ui->lineEdit_4);
                softKeyboard->resize(600,320);

                // 计算softKeyboard的顶部位置
                int newY = point.y()+80;
                // 移动softKeyboard到lineEdit的上方
                softKeyboard->move(point.x() , newY);
                softKeyboard->setMoveEnabled(false);
                softKeyboard->show();
            }
        }


    }
    return QMainWindow::eventFilter(watched,event);//返回父类的事件过滤器
}



//添加车位
void InfoInquire::on_pushButton_4_clicked()
{
    int succeed=0;
    for(int i=0;i<ui->lineEdit_4->text().toInt();i++)
    {

        //打包sql插入语句
        QString insertSql=QString("insert into parkingstatus values('%1%2%3','空闲');")
                .arg(ui->lineEdit_2->text())
                .arg(ui->lineEdit_3->text())
                .arg(QString::asprintf("%02d",i+1));
        qDebug()<<insertSql;
        //执行sql语句
        QSqlQuery query;  //自动关联到默认连接的数据上
        if(!query.exec(insertSql))
        {
            QString error = QString("添加失败:%1%2%3车位已拥有")
                    .arg(ui->lineEdit_2->text())
                    .arg(ui->lineEdit_3->text())
                    .arg(QString::asprintf("%02d",i+1));
            QMessageBox::warning(this,"sql",error);
            qDebug()<<query.lastError().text();
        }
        else
        {
            succeed++;
        }
    }
    if(succeed==ui->lineEdit_4->text().toInt())
    {
       QMessageBox::warning(this,"sql","添加成功");
    }

}
